package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Resource
    private BrandService brandService;
    @Resource
    private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据分类id获取品牌信息*********
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> brandRelationEntities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId));
        List<BrandEntity> brandEntityList = brandRelationEntities.stream()
                .map(item -> {
                    Long brandId = item.getBrandId();
                    BrandEntity brandEntity = brandService.getById(brandId);
                    return brandEntity;
                }).collect(Collectors.toList());
        return brandEntityList;
    }

    /**
     * 更新品牌信息***
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        // 根据brandId，找到是哪个CategoryBrandRelationEntity
        List<CategoryBrandRelationEntity> categoryBrandRelationList = this.list(queryWrapper);
        // 跟新找到是哪个CategoryBrandRelationEntity
        List<CategoryBrandRelationEntity> brandRelationEntityList = categoryBrandRelationList.stream()
                .map(categoryBrandRelation -> {
                    categoryBrandRelation.setBrandId(brandId);
                    categoryBrandRelation.setBrandName(name);
                    return categoryBrandRelation;
                }).collect(Collectors.toList());

        this.updateBatchById(brandRelationEntityList);
    }

    /**
     * 更新分类信息***
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
//        this.baseMapper.updateCategory(catId,name);
//        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<CategoryBrandRelationEntity>()
//                .eq("catelog_id", catId);
//        CategoryBrandRelationEntity categoryBrandRelation = this.getOne(queryWrapper);
//        categoryBrandRelation.setCatelogName(name);
//        this.updateById(categoryBrandRelation);
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId)
                .set("catelog_name", name);
        this.update(null, updateWrapper);
    }

    /**
     * 保存*****
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

}